<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="初期信息">
    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">
    <style type="text/css">
    </style>
    <script>
        let option = {
            elem: '#selectHouse'
            ,placeholder:"全部"
            ,width:160
            ,height:38
            , value: "0"
            ,clear:true
            ,chooseData:["${storeId}"]
            , getChildren: function (value, callback) {
                var data = [];
                //表示是门店
                if (value == "0"){
                    get("${baseUrl}organization/store/getStores.action", function (res) {
                        if (res.code == 1){
                            data.push({value:"", label:"全部"});
                            $.each(res.data, function (i,d) {
                                data.push({value:d.id, label:d.name, hasChild:true});
                            });
                            callback(data);
                        }
                    });
                } else {
                    //表示是仓库
                    get("${baseUrl}organization/house/getHouseByStore.action?storeId="+value, function (res) {
                        if (res.code == 1){
                            data.push({value:"", label:"全部"});
                            $.each(res.data, function (i,d) {
                                data.push({value:d.id, label:d.name});
                            });

                            callback(data);
                        }
                    });
                }
            }
        };

        jQuery(function () {

            fixTableOperatorButton();
            layui.use(['form', 'layedit','table', 'laydate','element','ajaxCascader','openTable'], function(){
                var form = layui.form
                    ,layer = layui.layer
                    ,table = layui.table
                    ,openTable = layui.openTable
                    ,layedit = layui.layedit
                    ,cascader = layui.ajaxCascader
                    ,laydate = layui.laydate;


                renderGoodsTable();
                renderCustomTable();
                renderProviderTable();
                renderSettAccountTable();

                // Ajax传参模式
                cascader.load(option);
                // 选择器点击事件的监听
                cascader.on('click', '#selectHouse', function(data){
                    let datas = cascader.getChooseData();
                    $("[name='storeId']").val(datas[0]);
                    $("[name='houseId']").val(datas[1]);


                    table.reload('table_goods',{where: getFormJson($("#form_goods")),page: {curr: 1}});
                });
                $(".cascader-clear").click(function () {
                    $("[name='houseId']").val("");
                    $("[name='storeId']").val("");


                    table.reload('table_goods',{where: getFormJson($("#form_goods")),page: {curr: 1}});
                });

                $("#btn-search-goods").click(function () {
                    table.reload('table_goods',{where: getFormJson($("#form_goods")),page: {curr: 1}});
                });
                $("#btn-search-custom").click(function () {
                    table.reload('table_custom',{where: getFormJson($("#form_custom")),page: {curr: 1}});
                });
                $("#btn-search-provider").click(function () {
                    table.reload('table_provider',{where: getFormJson($("#form_provider")),page: {curr: 1}});
                });
                $("#btn-search-settAccount").click(function () {
                    table.reload('table_settAccount',{where: getFormJson($("#form_settAccount")),page: {curr: 1}});
                });

                table.on('tool(table_goods)', function (obj) {
                    let data = obj.data;
                    switch(obj.event){
                        case 'detail':
                            openTab("商品<"+data.name+">详情","${baseUrl}goods/goodsDetail.action?id="+data.id);
                            break;
                        case 'house':
                            openHouseStockDialog(data);
                            break;
                    };
                });

                function renderGoodsTable() {
                    table.render({
                        id:'table_goods',
                        elem: '#table_goods',
                        url: "${baseUrl}sett/initInfo/getGoodsInitList.action",
                        where : {
                            time:(new Date()).valueOf()
                        },
                        cols: [[
                            {width:70,title:"序号",totalRowText: '合计',templet:function (d) {
                                    return genTableIndex(d.status == 2, "停用", d);
                                }},
                            {field: 'no', width: 150, title: '编号',style:"font-size:12px;"},
                            {field: 'name', minWidth: 120, title: '名称',style:"font-size:12px;"},
                            {field: 'type', width: 130, title: '分类',style:"font-size:12px;",templet: function(d){
                                    return d.type.name;
                                }},
                            {field: 'norms', width: 120, title: '规格',style:"font-size:12px;"},
                            {width: 120, title: '属性',style:"font-size:12px;",templet: function(d){
                                    return genAttr(d.attrNameVoList);
                                }},
                            {width: 80, title: '单位',style:"font-size:12px;",templet: function(d){
                                    return d.goodsUnitVoList[0].unit.name;
                                }},
                            {width: 130, title: '初期库存数量',style:"font-size:12px;",templet: function(d){
                                    let init = 0;
                                    $.each(d.goodsInitVoList, function (i,item) {
                                        init += item.initNum;
                                    });
                                    return getNumber(init);
                                }},

                            {width: 130, title: '初期库存金额',style:"font-size:12px;",templet: function(d){
                                    let init = 0;
                                    $.each(d.goodsInitVoList, function (i,item) {
                                        init += item.initAmt;
                                    });
                                    return getNumber(init);
                                }},
                            {title: '操作', minWidth: 80,fixed:'right', templet: '#goods_operBar'},
                        ]],
                        limit: 10,
                        page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                            //,curr: 5 //设定初始在第 5 页
                            ,groups: 5 //只显示 5 个连续页码
                            ,first: false //不显示首页
                            ,last: false //不显示尾页
                        },
                        done: function(res, curr, count){
                            setTableLineCss("table",res.data, "status", 2, "font-gray-c2c2c2");
                        }
                    });
                }

                function renderCustomTable() {
                    openTable.render({
                        id:'table_custom'
                        , elem: '#table_custom'
                        , url: "${baseUrl}sett/initInfo/getCustomInitList.action?time="+(new Date()).valueOf()
                        , limit: 10
                        , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                            //,curr: 5 //设定初始在第 5 页
                            ,groups: 5 //只显示 5 个连续页码
                            ,first: false //不显示首页
                            ,last: false //不显示尾页
                        }
                        , done: function(res, curr, count){
                        }
                        , cols: [[
                            {width:70,title:"序号",totalRowText: '合计',templet:function (d) {
                                    return d.LAY_INDEX;
                                }},
                            {field: 'no', width: 130, title: '编号',style:"font-size:12px;"},
                            {field: 'name', width: 150, title: '名称',style:"font-size:12px;"},
                            {width: 120, title: '初期欠款',style:"font-size:12px;",templet:function (d) {
                                    return getPrice(d.initAmt);
                                }},
                            {field: 'contacts', width: 120, title: '联系人',style:"font-size:12px;"},
                            {field: 'phone', width: 120, title: '联系电话',style:"font-size:12px;"},
                            {width: 120, title: '分类',style:"font-size:12px;",templet:function (d) {
                                    return d.customType.name;
                                }},
                            {width: 120, title: '门店',style:"font-size:12px;",templet:function (d) {
                                    return d.store==null ? '':d.store.name;
                                }},
                            {field: 'remark', minWidth: 120, title: '备注',style:"font-size:12px;"},
                        ]]

                        //注：展开的列Items
                        , openCols: [
                            {field: 'no', title: '编号'},
                            {field: 'name', title: '名称'},

                            {title: '应收欠款',templet: function(d){
                                    return genOpenTableContent("初期欠款",getPrice(d.initAmt));
                                }},
                            {title: '关联业务员',templet: function(d){
                                    return genOpenTableContent("关联业务员",d.user == null ? "" : d.user.name);
                                }},
                            {field: 'unitTel',title: '单位电话',
                                templet: function(d){
                                    return genOpenTableContent("单位电话",d.unitTel == null ? "" : d.unitTel);
                                }},
                            {field: 'contacts',title: '联系人',
                                templet: function(d){
                                    return genOpenTableContent("联系人",d.contacts == null ? "" : d.contacts);
                                }},
                            {field: 'phone',title: '联系电话',
                                templet: function(d){
                                    return genOpenTableContent("联系电话",d.phone == null ? "" : d.phone);
                                }},
                            {title: '分类',
                                templet: function(d){
                                    return genOpenTableContent("分类",d.customType.name);
                                }
                            },
                            {title: '状态',
                                templet: function(d){
                                    return genOpenTableContent("状态",d.status == '1' ? "启用":"停用");
                                }
                            },
                            {title: '默认',
                                templet: function(d){
                                    return genOpenTableContent("默认",isTrue(d.def) ? "是":"否");
                                }
                            },
                            {field: 'wechat',title: '微信',
                                templet: function(d){
                                    return genOpenTableContent("微信",d.wechat == null ? "" : d.wechat);
                                }
                            },
                            {field: 'qq',title: 'QQ',
                                templet: function(d){
                                    return genOpenTableContent("QQ",d.qq == null ? "" : d.qq);
                                }},
                            {field: 'tax',title: '传真',
                                templet: function(d){
                                    return genOpenTableContent("传真",d.tax == null ? "" : d.tax);
                                }},
                            {field: 'email',title: '邮件',
                                templet: function(d){
                                    return genOpenTableContent("邮件",d.email == null ? "" : d.email);
                                }},
                            {field: 'code',title: '邮编',
                                templet: function(d){
                                    return genOpenTableContent("邮编",d.code == null ? "" : d.code);
                                }},
                            {field: 'address',title: '地址',
                                templet: function(d){
                                    return genOpenTableContent("地址",d.address == null ? "" : d.address);
                                }},
                            {field: 'remark', title: '备注',
                                templet: function(d){
                                    return genOpenTableContent("备注",d.remark == null ? "" : d.remark);
                                }},
                            {title: '门店',templet:function (d) {
                                    return genOpenTableContent("门店",d.store==null ? '':d.store.name);
                                }},
                        ]
                    });
                }

                function renderProviderTable() {
                    openTable.render({
                        id:'table_provider'
                        , elem: '#table_provider'
                        , url: "${baseUrl}sett/initInfo/getProviderInitList.action?time="+(new Date()).valueOf()
                        , limit: 10
                        , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                            //,curr: 5 //设定初始在第 5 页
                            ,groups: 5 //只显示 5 个连续页码
                            ,first: false //不显示首页
                            ,last: false //不显示尾页
                        }
                        , done: function(res, curr, count){
                        }
                        , cols: [[
                            {width:70,title:"序号",totalRowText: '合计',templet:function (d) {
                                    return d.LAY_INDEX;
                                }},
                            {field: 'no', width: 130, title: '编号',style:"font-size:12px;"},
                            {field: 'name', width: 150, title: '名称',style:"font-size:12px;"},
                            {width: 120, title: '初期欠款',style:"font-size:12px;",templet:function (d) {
                                    return getPrice(d.initAmt);
                                }},
                            {field: 'contacts', width: 120, title: '联系人',style:"font-size:12px;"},
                            {field: 'phone', width: 120, title: '联系电话',style:"font-size:12px;"},
                            {width: 120, title: '分类',style:"font-size:12px;",templet:function (d) {
                                    return d.providerType.name;
                                }},
                            {width: 120, title: '门店',style:"font-size:12px;",templet:function (d) {
                                    return d.store==null ? '':d.store.name;
                                }},
                            {field: 'remark', minWidth: 120, title: '备注',style:"font-size:12px;"},
                        ]]

                        //注：展开的列Items
                        , openCols: [
                            {field: 'no', title: '编号'},
                            {field: 'name', title: '名称'},
                            {title: '初期欠款',templet: function(d){
                                    return genOpenTableContent("初期欠款",getPrice(d.initAmt));
                                }},
                            {field: 'unitTel',title: '单位电话'},
                            {field: 'contacts',title: '联系人'},
                            {field: 'phone',title: '联系电话'},
                            {title: '分类',
                                templet: function(d){
                                    return genOpenTableContent("分类",d.providerType.name);
                                }
                            },
                            {title: '状态',
                                templet: function(d){
                                    return genOpenTableContent("状态",d.status == '1' ? "启用":"停用");
                                }
                            },
                            {title: '默认',
                                templet: function(d){
                                    return genOpenTableContent("默认",isTrue(d.def) ? "是":"否");
                                }
                            },
                            {field: 'wechat',title: '微信',
                                templet: function(d){
                                    return genOpenTableContent("微信",d.wechat == null ? "" : d.wechat);
                                }
                            },
                            {field: 'qq',title: 'QQ',
                                templet: function(d){
                                    return genOpenTableContent("QQ",d.qq == null ? "" : d.qq);
                                }},
                            {field: 'tax',title: '传真',
                                templet: function(d){
                                    return genOpenTableContent("传真",d.tax == null ? "" : d.tax);
                                }},
                            {field: 'email',title: '邮件',
                                templet: function(d){
                                    return genOpenTableContent("邮件",d.email == null ? "" : d.email);
                                }},
                            {field: 'code',title: '邮编',
                                templet: function(d){
                                    return genOpenTableContent("邮编",d.code == null ? "" : d.code);
                                }},
                            {field: 'address',title: '地址',
                                templet: function(d){
                                    return genOpenTableContent("地址",d.address == null ? "" : d.address);
                                }},
                            {title: '门店',templet:function (d) {
                                    return genOpenTableContent("门店",d.store==null ? '':d.store.name);
                                }},
                            {field: 'remark', title: '备注',
                                templet: function(d){
                                    return genOpenTableContent("备注",d.remark == null ? "" : d.remark);
                                }},
                        ]
                    });
                }

                function renderSettAccountTable() {
                    openTable.render({
                        id:'table_settAccount'
                        , elem: '#table_settAccount'
                        , url: "${baseUrl}sett/initInfo/getSettAccountInitList.action?time="+(new Date()).valueOf()
                        , limit: 10
                        , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                            //,curr: 5 //设定初始在第 5 页
                            ,groups: 5 //只显示 5 个连续页码
                            ,first: false //不显示首页
                            ,last: false //不显示尾页
                        }
                        , done: function(res, curr, count){

                        }
                        , cols: [[
                            {width:70,title:"序号",templet:function (d) {
                                    return genTableIndexGreen(d.def, "默认", d);
                                }},
                            {field: 'id', width: 10, title: 'ID',hide:true},
                            {field: 'name', width: 150, title: '账户名称',style:"font-size:12px;"},
                            {field: 'openBank', width: 150, title: '开户银行',style:"font-size:12px;"},
                            {width: 110, title: '初期余额',style:"font-size:12px;",
                                templet: function(d){
                                    return getPrice(d.initBalance);
                                }
                            },
                            {field:"store", title: '所属门店',style:"font-size:12px;",
                                templet: function(d){
                                    return d.store.name;
                                }
                            },
                        ]]

                        //注：展开的列Items
                        , openCols: [
                            {field: 'name', title: '账户名称'},
                            {field: 'openBank',title: '开户银行'},
                            {title: '所属门店',
                                templet: function(d){
                                    return genOpenTableContent("所属门店",d.store.name);
                                }
                            },
                            {title: '默认',
                                templet: function(d){
                                    return genOpenTableContent("默认",isTrue(d.def) ? "是":"否");
                                }
                            },
                            {title: '初期余额',
                                templet: function(d){
                                    return genOpenTableContent("初期余额",getPrice(d.initBalance));
                                }
                            },
                            {title: '银行卡号',
                                templet: function(d){
                                    return genOpenTableContent("银行卡号",d.card=="" ? "":d.card);
                                }
                            },
                            {field: 'remark', title: '备注'},
                        ]
                    });
                }
            });
        });

        function openHouseStockDialog(data) {
            layui.use(['laytpl'], function () {
                let $ = layui.jquery,
                    laytpl = layui.laytpl,
                    util = layui.util;

                let area;
                if (document.documentElement.clientWidth > 600){
                    area = ['550px', (document.documentElement.clientHeight-40)+'px'];
                } else {
                    area = [(document.documentElement.clientWidth-8)+'px', (document.documentElement.clientHeight-8)+'px'];
                }
                area='550px';

                var getTpl = stockByHouseBar.innerHTML;
                laytpl(getTpl).render(data, function(html){
                    let index = layer.open({
                        title:'库存分布',
                        area: area,
                        skin: 'layui-layer-molv',
                        type: 1,
                        content: html,
                        btn: ['关闭'],
                        yes: function(index, layero){
                            closeDialog(index);
                        },
                    });
                });

            });
        }
    </script>
</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-tab layui-tab-brief">
                <ul class="layui-tab-title">
                    <li class="layui-this">初期商品库存</li>
                    <li>初期应收欠款</li>
                    <li>初期应付欠款</li>
                    <li>初期账户余额</li>
                </ul>
                <div class="layui-tab-content">
                    <div class="layui-tab-item layui-show">
                        <form class="layui-form layui-form-pane" id="form_goods">
                            <input type="hidden" name="houseId">
                            <input type="hidden" name="storeId">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">门店仓库</label>
                                    <div class="layui-input-inline">
                                        <cascader id="selectHouse"></cascader>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">搜索信息</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="search" placeholder="编号、名称、规格、条码" class="layui-input font12" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <a class="layui-btn " id="btn-search-goods"><i class="layui-icon layui-icon-search"></i></a>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table class="layui-hide" id="table_goods" lay-filter="table_goods" lay-size="sm"></table>
                    </div>

                    <div class="layui-tab-item">
                        <form class="layui-form layui-form-pane" id="form_custom">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">搜索信息</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="search" placeholder="编号、名称、联系人、电话" class="layui-input font12" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <a class="layui-btn " id="btn-search-custom"><i class="layui-icon layui-icon-search"></i></a>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table class="layui-hide" id="table_custom" lay-filter="table_custom" lay-size="sm"></table>
                    </div>

                    <div class="layui-tab-item">
                        <form class="layui-form layui-form-pane" id="form_provider">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">搜索信息</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="search" placeholder="编号、名称、联系人、电话" class="layui-input font12" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <a class="layui-btn " id="btn-search-provider"><i class="layui-icon layui-icon-search"></i></a>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table class="layui-hide" id="table_provider" lay-filter="table_provider" lay-size="sm"></table>
                    </div>

                    <div class="layui-tab-item">
                        <form class="layui-form layui-form-pane" id="form_settAccount">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">搜索信息</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="search" placeholder="名称、开户行" class="layui-input font12" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <div class="layui-input-inline">
                                        <a class="layui-btn " id="btn-search-settAccount"><i class="layui-icon layui-icon-search"></i></a>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <table class="layui-hide" id="table_settAccount" lay-filter="table_settAccount" lay-size="sm"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="goods_operBar">
    <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="detail">详情</a>
    <a class="layui-btn layui-btn-xs table-operator" lay-event="house">库存分布</a>
</script>


<script type="text/html" id="stockByHouseBar">
    <table class="layui-table" style="margin: 0">
        <thead>
        <tr>
            <th>序号</th>
            <th>门店</th>
            <th>仓库</th>
            <th>初期数量</th>
            <th>初期成本</th>
            <th>初期金额</th>
        </tr>
        </thead>

        <tbody>
        {{#  layui.each(d.goodsInitVoList, function(index, item){ }}
        <tr>
            <td>{{index+1}}</td>
            <td style="font-size: 12px;color: #666;">{{ item.houseVo.store.name }}</td>
            <td style="font-size: 12px;color: #666;">{{ item.houseVo.name }}</td>
            <td style="color: #666;">{{ getNumber(item.initNum) }}</td>
            <td style="color: #666;">{{ getPrice(item.initCost) }}</td>
            <td style="color: #666;">{{ getPrice(item.initAmt) }}</td>
        </tr>
        {{#  }); }}
        </tbody>
    </table>
</script>
</html>